declare @login varchar(128), @sqlstr varchar(2000) set @login = 'BOULDER\IIS-DM' set @login = replace(replace(@login, '[', ''), ']', '') select @sqlstr = 'Removing login [' + name + '] from server ' + @@servername from sysxlogins where name = @login print @sqlstr; print '' if @sqlstr is null begin Select @sqlstr = 'login [' + @login + '] not found on server ' + @@servername print @sqlstr end else begin set @sqlstr = 'use ? if exists (select 1 from dbo.sysusers u inner join master.dbo.sysxlogins l on u.sid = l.sid where l.name = ''' + @login + ''') begin declare @sqlstr varchar(2000) select @sqlstr = ''Removing user ['' + u.name + ''] from database '' + db_name() from sysusers u inner join master.dbo.sysxlogins l on u.sid = l.sid where l.name = ''' + @login + ''' print @sqlstr exec sp_revokedbaccess [' + @login + '] print '''' end' exec sp_msforeachdb @sqlstr exec sp_revokelogin @login end